package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-13
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private MinIOService minIOService;

    @Override
    public ResponseResult upload(MultipartFile file) {

        User user = MediaThreadLocalUtil.get();

        System.out.println("用户id是" + user.getUserId());

        if (user == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        try {
            String upload = minIOService.upload(file);

            WmMaterial wmMaterial = new WmMaterial();

            wmMaterial.setCreatedTime(new Date());

            wmMaterial.setUserId(user.getUserId());

            wmMaterial.setIsCollection(false);

            wmMaterial.setUrl(upload);

            wmMaterial.setType(0);

            this.save(wmMaterial);

            return ResponseResult.okResult(wmMaterial);
        } catch (Exception e) {
            e.printStackTrace();

            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult listByCollection(WmMaterialDto dto) {

        System.out.println("这是一个请求进来了");

        User user = MediaThreadLocalUtil.get();

        if(user==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();

        if(dto.getIsCollection()!=null){

            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        queryWrapper.eq(WmMaterial::getUserId,user.getUserId());

        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        IPage<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());

        IPage<WmMaterial> page1 = this.page(page, queryWrapper);

        return new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());
    }
}
